package thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class MyData
{
    volatile int number = 0;

    public void addTo60()
    {
        this.number =60;
    }
}

/**
 * volatile 可见性代码验证
 * number 加volatile验证
 */
public class VolatileDemo {
    public static void main(String[] args) {
        MyData myData = new MyData();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+"\t come in");
            try{
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myData.addTo60();
            System.out.println(Thread.currentThread().getName()+"\t update number value:"+myData.number);
        },"t1").start();

        while (myData.number == 0){

        }

        System.out.println(Thread.currentThread().getName()+"\t misssion is over,main get number value:"+myData);
    }
}
